package com.baoleme.service.impl;

import com.baoleme.entity.OrderDetail;
import com.baoleme.entity.Orders;
import com.baoleme.entity.dto.OrderConditionalSearchDto;
import com.baoleme.entity.dto.OrderStatusDto;
import com.baoleme.entity.vo.OrderDetailVo;
import com.baoleme.exceptions.BusinessException;
import com.baoleme.exceptions.enumeration.ResponseEnum;
import com.baoleme.mapper.OrdersMapper;
import com.baoleme.service.OrderDetailService;
import com.baoleme.service.OrdersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @since 2024-09-13
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Resource
    private OrderDetailService orderDetailService;

    public IPage<Orders> conditionSearch(@Validated OrderConditionalSearchDto OrderConditionalSearchDto) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        // 条件查询
        if (OrderConditionalSearchDto.getId() != null && !"".equals(OrderConditionalSearchDto.getId())) {
            wrapper.like(Orders::getId, OrderConditionalSearchDto.getId());
        }
        if (OrderConditionalSearchDto.getStatus() != null && OrderConditionalSearchDto.getStatus() > 0) {
            wrapper.eq(Orders::getStatus, OrderConditionalSearchDto.getStatus());
        }
        if (OrderConditionalSearchDto.getPayMethod() != null && OrderConditionalSearchDto.getPayMethod() > 0) {
            wrapper.eq(Orders::getPayMethod, OrderConditionalSearchDto.getPayMethod());
        }
        if (OrderConditionalSearchDto.getLoTime() != null) {
            wrapper.gt(Orders::getCheckoutTime, OrderConditionalSearchDto.getLoTime());
        }
        if (OrderConditionalSearchDto.getHiTime() != null) {
            wrapper.lt(Orders::getCheckoutTime, OrderConditionalSearchDto.getHiTime());
        }
        IPage<Orders> pageInfo = new Page<>(OrderConditionalSearchDto.getCurrent(), OrderConditionalSearchDto.getPageSize());
        return this.page(pageInfo, wrapper);
    }

    @Override
    public HashMap<String, Long> statistics() {
        LambdaQueryWrapper<Orders> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Orders::getStatus, 1);
        long count1 = this.count(wrapper1);
        LambdaQueryWrapper<Orders> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Orders::getStatus, 2);
        long count2 = this.count(wrapper2);
        LambdaQueryWrapper<Orders> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(Orders::getStatus, 3);
        long count3 = this.count(wrapper3);
        LambdaQueryWrapper<Orders> wrapper4 = new LambdaQueryWrapper<>();
        wrapper4.eq(Orders::getStatus, 4);
        long count4 = this.count(wrapper4);
        LambdaQueryWrapper<Orders> wrapper5 = new LambdaQueryWrapper<>();
        wrapper5.eq(Orders::getStatus, 5);
        long count5 = this.count(wrapper5);
        LambdaQueryWrapper<Orders> wrapper6 = new LambdaQueryWrapper<>();
        wrapper6.eq(Orders::getStatus, 6);
        long count6 = this.count(wrapper6);
        HashMap<String, Long> statusCount = new HashMap<>();
        statusCount.put("paying", count1);
        statusCount.put("waitingDelivery", count2);
        statusCount.put("distributing", count3);
        statusCount.put("complete", count4);
        statusCount.put("canceling", count5);
        statusCount.put("canceled", count6);
        return statusCount;
    }

    @Override
    public OrderDetailVo details(String id) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getId, id);
        Orders order = this.getOne(wrapper);
        if (order != null) {
            LambdaQueryWrapper<OrderDetail> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(OrderDetail::getOrderId, id);
            List<OrderDetail> orderDetails = orderDetailService.list(wrapper2);
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setOrder(order);
            orderDetailVo.setOrderDetails(orderDetails);
            return orderDetailVo;
        } else {
            throw new BusinessException(ResponseEnum.ORDER_NOT_FOUND);
        }
    }

    @Override
    public Boolean modifyOrdersStatus(OrderStatusDto orderStatusDto) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getId, orderStatusDto.getId());
        if (this.count(wrapper) == 0) {
            throw new BusinessException(ResponseEnum.ORDER_NOT_FOUND);
        }
        Orders orders = new Orders();
        orders.setStatus(orderStatusDto.getStatus());
        if (orderStatusDto.getRemark() != null && !"".equals(orderStatusDto.getRemark())) {
            orders.setRemark(orderStatusDto.getRemark());
        }
        return this.update(orders, wrapper);
    }

    @Override
    public List<Orders> queryOrdersByUserId(String userId) {
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getMemberId, userId);
        if (this.count(wrapper) > 0) {
            return this.list(wrapper);
        } else {
            throw new BusinessException(ResponseEnum.NO_HISTORY_ORDER);
        }
    }
}